package com.jmgd.cjlx.service.impl;

import com.jmgd.cjlx.dao.GdjfMapper;
import com.jmgd.cjlx.service.GdjfService;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class GdjfServiceImpl implements GdjfService {
    @Autowired
    GdjfMapper gdjfMapper;



    @Override
    @Async
    public void createExcelSaleYJ(Date beginDate, Date endDate) throws ParseException {
        sale(beginDate,endDate);
    }

    private void sale(Date beginDate, Date endDate) throws ParseException {

        while(beginDate.compareTo(endDate)<0) {
            List<Map<String, Object>> saleYJHList = null;
            saleYJHList = gdjfMapper.selectExcelSaleYJH(beginDate, dateAddOne(beginDate,1));
            if (saleYJHList.size()>0) {
                //List<Map<String,Object>> saleYJDList = jmgdMapper.selectExcelSaleYJD(beginDate,endDate);
                SimpleDateFormat format3 = new SimpleDateFormat("yyyyMMdd");

                SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");

                // 1.声明文件名称
                String fileName = "pifachuku_jiangmen";
                String stockName = "";
                //String fileName1 = "销售出库";
                    stockName = "零售仓";
                    fileName = "零售" + fileName;
                //else if(stockNo.equals("50306")){
                //  stockName = "50306临时仓";
                //}


                fileName = fileName + format3.format(beginDate);
                fileName = "D:\\GDFiles\\Yj\\JF\\" + stockName + "\\" + fileName;


                // 2.创建dom对象
                Document document = DocumentHelper.createDocument();

                // 3.添加节点，根据需求添加，这里我只是设置了一个head节点，下面有name和age两个子节点
                Element head = document.addElement("jh", "http://cmsland.com/yjj_qyjk_pfck.xsd");
                //head.addAttribute("xmlns", "http://cmsland.com/yjj_qyjk_pfck.xsd");
                for (Map<String, Object> map : saleYJHList) {
                    System.out.println(map.get("SALENO") == null ? "" : map.get("SALENO").toString());
                    String[] arr1 = new String[10];//有几列就写几
                    //如果数据不为空则赋值(这里就是循环每行的数据)
                    Element dj = head.addElement("dj");
                    dj.addAttribute("djbh", map.get("SALENO").toString());
                    dj.addAttribute("ckrq", format1.format(format2.parse(map.get("SALE_DATE").toString())));
                    dj.addAttribute("cgqy", map.get("VENCUS_NAME").toString());

                    List<Map<String, Object>> saleYJDList = gdjfMapper.selectExcelSaleYJD(map.get("SALENO").toString(), format2.parse(map.get("SALE_DATE").toString()));
                    for (Map<String, Object> mapd : saleYJDList) {
                        Element tm = dj.addElement("tm");
                        tm.addAttribute("bwm", mapd.get("BWM") == null ? "" : mapd.get("BWM").toString());
                        tm.addAttribute("bzgg", mapd.get("WARE_SPEC") == null ? "" : mapd.get("WARE_SPEC").toString());
                        tm.addAttribute("scph", mapd.get("MAKENO") == null ? "" : mapd.get("MAKENO").toString());
                        tm.addAttribute("scrq", mapd.get("PROD_DATE") == null ? "" : format1.format(format2.parse(mapd.get("PROD_DATE").toString())));
                        tm.addAttribute("yxqz", mapd.get("VALID_DATE") == null ? "" : format1.format(format2.parse(mapd.get("VALID_DATE").toString())));
                        tm.addAttribute("sl", mapd.get("WARE_QTY") == null ? "" : mapd.get("WARE_QTY").toString());
                    }
/*

            arr1[0] = map.get("SALE_DATE") == null ? "" : format1.format(format2.parse(map.get("SALE_DATE").toString()));
            arr1[1] = map.get("SALENO") == null ? "" : map.get("SALENO").toString();
            arr1[2] = map.get("VENCUS_NAME") == null ? "" : map.get("VENCUS_NAME").toString();
            arr1[3] = map.get("SALER") == null ? "" : map.get("SALER").toString();
            arr1[4] = map.get("TRANS_TYPE") == null ? "" : map.get("TRANS_TYPE").toString();
            arr1[5] = map.get("CHENG_YUN_PERSON") == null ? "" : map.get("CHENG_YUN_PERSON").toString();
            arr1[6] = map.get("BEGIN_DATE") == null ? "" : format1.format(format2.parse(map.get("BEGIN_DATE").toString()));
            arr1[7] = map.get("END_DATE") == null ? "" : format1.format(format2.parse(map.get("END_DATE").toString()));
            arr1[8] = map.get("STARTUP_TEMP") == null ? "" : map.get("STARTUP_TEMP").toString();
            arr1[9] = map.get("ARRI_TEMP") == null ? "" : map.get("ARRI_TEMP").toString();*/
                }


                // Element age = head.addElement("age");
                //age.addText("18");

                // 4、格式化模板
                //OutputFormat format = OutputFormat.createCompactFormat();
                OutputFormat format = OutputFormat.createPrettyPrint();
                format.setEncoding("GBK");

                // 5、生成xml文件
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                try {
                    XMLWriter writer = new XMLWriter(out, format);
                    writer.write(document);
                    writer.close();
                } catch (IOException e) {
                    System.out.println("生成xml文件失败。文件名【" + fileName + "】");
                }

                // 6、生成的XML文件
                // 7、利用文件输出流输出到文件， 文件输出到了您的项目根目录下了
                try (FileOutputStream fos = new FileOutputStream(fileName + ".xml")) {
                    fos.write(out.toByteArray());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            //System.out.println("生成药监批发配送xml文件成功");
            beginDate = dateAddOne(beginDate,1);
        }
    }


    @Override
    @Async
    public void createExcelSaleReturnYJ(Date beginDate, Date endDate) throws ParseException {
        returnSale(beginDate, endDate,"50301");
        returnSale(beginDate, endDate,"0");
        //returnSale(beginDate, endDate,"6");
    }

    private void returnSale(Date beginDate, Date endDate,String stockNo) throws ParseException {
        while(beginDate.compareTo(endDate)<0) {
        List<Map<String, Object>> saleYJHList = null;

            saleYJHList = gdjfMapper.selectExcelReturnSaleYJH(beginDate, dateAddOne(beginDate,1));

        if(saleYJHList.size()>0) {
            //List<Map<String,Object>> saleYJDList = jmgdMapper.selectExcelSaleYJD(beginDate,endDate);
            SimpleDateFormat format3 = new SimpleDateFormat("yyyyMMdd");

            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");

            // 1.声明文件名称
            String fileName = "pifatuihuoruku_jiangmen";
            //String fileName1 = "销售出库";
            String stockName = "";
            //String fileName1 = "销售出库";

            if (stockNo.equals("50301")) {
                stockName = "零售仓";
                fileName = "零售" + fileName;
            } else {
                stockName = "批发仓";
                fileName = "批发" + fileName;
            }
            //else if(stockNo.equals("50306")){
            //  stockName = "50306临时仓";
            //}


            fileName = fileName + format3.format(beginDate);
            fileName = "D:\\GDFiles\\Yj\\JF\\" + stockName + "\\" + fileName;

            // 2.创建dom对象
            Document document = DocumentHelper.createDocument();

            // 3.添加节点，根据需求添加，这里我只是设置了一个head节点，下面有name和age两个子节点
            Element head = document.addElement("jh", "http://cmsland.com/yjj_qyjk_pfthrk.xsd");
            //head.addAttribute("xmlns", "http://cmsland.com/yjj_qyjk_pfck.xsd");
            for (Map<String, Object> map : saleYJHList) {
                System.out.println(map.get("SALENO") == null ? "" : map.get("SALENO").toString());

                String[] arr1 = new String[10];//有几列就写几
                //如果数据不为空则赋值(这里就是循环每行的数据)
                Element dj = head.addElement("dj");
                dj.addAttribute("djbh", map.get("SALENO").toString());
                dj.addAttribute("rqrq", format1.format(format2.parse(map.get("SALE_DATE").toString())));
                dj.addAttribute("thqy", map.get("VENCUS_NAME").toString());
                dj.addAttribute("clr", "李协灿");
                dj.addAttribute("ysjl", "正常");

                List<Map<String, Object>> saleYJDList = gdjfMapper.selectExcelReturnSaleYJD(map.get("SALENO").toString(), format2.parse(map.get("SALE_DATE").toString()));
                for (Map<String, Object> map1 : saleYJDList) {
                    Element tm = dj.addElement("tm");
                    tm.addAttribute("bwm", map1.get("BWM") == null ? "" : map1.get("BWM").toString());
                    tm.addAttribute("bzgg", map1.get("WARE_SPEC") == null ? "" : map1.get("WARE_SPEC").toString());
                    tm.addAttribute("scph", map1.get("MAKENO") == null ? "" : map1.get("MAKENO").toString());
                    tm.addAttribute("scrq", map1.get("PROD_DATE") == null ? "" : format1.format(format2.parse(map1.get("PROD_DATE").toString())));
                    tm.addAttribute("yxqz", map1.get("VALID_DATE") == null ? "" : format1.format(format2.parse(map1.get("VALID_DATE").toString())));
                    tm.addAttribute("sl", map1.get("WARE_QTY") == null ? "" : map1.get("WARE_QTY").toString());
                    tm.addAttribute("yy", "配送退货");
                    tm.addAttribute("xsdh", map1.get("SALENO") == null ? "" : map1.get("SALENO").toString());
                }
/*

            arr1[0] = map.get("SALE_DATE") == null ? "" : format1.format(format2.parse(map.get("SALE_DATE").toString()));
            arr1[1] = map.get("SALENO") == null ? "" : map.get("SALENO").toString();
            arr1[2] = map.get("VENCUS_NAME") == null ? "" : map.get("VENCUS_NAME").toString();
            arr1[3] = map.get("SALER") == null ? "" : map.get("SALER").toString();
            arr1[4] = map.get("TRANS_TYPE") == null ? "" : map.get("TRANS_TYPE").toString();
            arr1[5] = map.get("CHENG_YUN_PERSON") == null ? "" : map.get("CHENG_YUN_PERSON").toString();
            arr1[6] = map.get("BEGIN_DATE") == null ? "" : format1.format(format2.parse(map.get("BEGIN_DATE").toString()));
            arr1[7] = map.get("END_DATE") == null ? "" : format1.format(format2.parse(map.get("END_DATE").toString()));
            arr1[8] = map.get("STARTUP_TEMP") == null ? "" : map.get("STARTUP_TEMP").toString();
            arr1[9] = map.get("ARRI_TEMP") == null ? "" : map.get("ARRI_TEMP").toString();*/
            }


            // Element age = head.addElement("age");
            //age.addText("18");

            // 4、格式化模板
            //OutputFormat format = OutputFormat.createCompactFormat();
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("GBK");

            // 5、生成xml文件
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            try {
                XMLWriter writer = new XMLWriter(out, format);
                writer.write(document);
                writer.close();
            } catch (IOException e) {
                System.out.println("生成xml文件失败。文件名【" + fileName + "】");
            }

            // 6、生成的XML文件
            // 7、利用文件输出流输出到文件， 文件输出到了您的项目根目录下了
            try (FileOutputStream fos = new FileOutputStream(fileName + ".xml")) {
                fos.write(out.toByteArray());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
            beginDate = dateAddOne(beginDate,1);

        }
        //System.out.println("生成销售退货xml文件成功");
    }

    @Override
    @Async
    public void createExcelPurYJ(Date beginDate, Date endDate) throws ParseException {
        pur(beginDate,endDate,"50301");
        pur(beginDate,endDate,"0");
    }

    private void pur(Date beginDate, Date endDate,String stockNo) throws ParseException {
        while(beginDate.compareTo(endDate)<0) {

            List<Map<String, Object>> saleYJHList = null;

            saleYJHList = gdjfMapper.selectExcelPurYJH(beginDate, dateAddOne(beginDate,1));

        if(saleYJHList.size()>0) {
            //List<Map<String,Object>> saleYJDList = jmgdMapper.selectExcelSaleYJD(beginDate,endDate);
            SimpleDateFormat format3 = new SimpleDateFormat("yyyyMMdd");

            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");

            // 1.声明文件名称
            String fileName = "caigouruku_jiangmen";
            //String fileName1 = "销售出库";

            String stockName = "";
            //String fileName1 = "销售出库";

            if (stockNo.equals("50301")) {
                stockName = "零售仓";
                fileName = "零售" + fileName;
            } else {
                stockName = "批发仓";
                fileName = "批发" + fileName;
            }


            fileName = fileName + format3.format(beginDate);
            fileName = "D:\\GDFiles\\Yj\\JF\\" + stockName + "\\" + fileName;


            // 2.创建dom对象
            Document document = DocumentHelper.createDocument();

            // 3.添加节点，根据需求添加，这里我只是设置了一个head节点，下面有name和age两个子节点
            Element head = document.addElement("jh", "http://cmsland.com/yjj_qyjk_cgrk.xsd");
            //head.addAttribute("xmlns", "http://cmsland.com/yjj_qyjk_pfck.xsd");
            for (Map<String, Object> map : saleYJHList) {
                System.out.println(map.get("SALENO") == null ? "" : map.get("SALENO").toString());

                String[] arr1 = new String[10];//有几列就写几
                //如果数据不为空则赋值(这里就是循环每行的数据)
                Element dj = head.addElement("dj");
                dj.addAttribute("djbh", map.get("SALENO").toString());
                dj.addAttribute("rqrq", format1.format(format2.parse(map.get("SALE_DATE").toString())));
                dj.addAttribute("ghqy", map.get("VENCUS_NAME").toString());
                dj.addAttribute("xsdh", map.get("SALENO").toString());
                dj.addAttribute("clr", "李协灿");
                dj.addAttribute("ysjl", "合格");

                List<Map<String, Object>> saleYJDList = gdjfMapper.selectExcelPurYJD(map.get("SALENO").toString(), format2.parse(map.get("SALE_DATE").toString()));
                for (Map<String, Object> map1 : saleYJDList) {
                    Element tm = dj.addElement("tm");
                    tm.addAttribute("bwm", map1.get("BWM") == null ? "" : map1.get("BWM").toString());
                    tm.addAttribute("bzgg", map1.get("WARE_SPEC") == null ? "" : map1.get("WARE_SPEC").toString());
                    tm.addAttribute("scph", map1.get("MAKENO") == null ? "" : map1.get("MAKENO").toString());
                    tm.addAttribute("scrq", map1.get("PROD_DATE") == null ? "" : format1.format(format2.parse(map1.get("PROD_DATE").toString())));
                    tm.addAttribute("yxqz", map1.get("VALID_DATE") == null ? "" : format1.format(format2.parse(map1.get("VALID_DATE").toString())));
                    tm.addAttribute("sl", map1.get("WARE_QTY") == null ? "" : map1.get("WARE_QTY").toString());
                }
/*

            arr1[0] = map.get("SALE_DATE") == null ? "" : format1.format(format2.parse(map.get("SALE_DATE").toString()));
            arr1[1] = map.get("SALENO") == null ? "" : map.get("SALENO").toString();
            arr1[2] = map.get("VENCUS_NAME") == null ? "" : map.get("VENCUS_NAME").toString();
            arr1[3] = map.get("SALER") == null ? "" : map.get("SALER").toString();
            arr1[4] = map.get("TRANS_TYPE") == null ? "" : map.get("TRANS_TYPE").toString();
            arr1[5] = map.get("CHENG_YUN_PERSON") == null ? "" : map.get("CHENG_YUN_PERSON").toString();
            arr1[6] = map.get("BEGIN_DATE") == null ? "" : format1.format(format2.parse(map.get("BEGIN_DATE").toString()));
            arr1[7] = map.get("END_DATE") == null ? "" : format1.format(format2.parse(map.get("END_DATE").toString()));
            arr1[8] = map.get("STARTUP_TEMP") == null ? "" : map.get("STARTUP_TEMP").toString();
            arr1[9] = map.get("ARRI_TEMP") == null ? "" : map.get("ARRI_TEMP").toString();*/
            }


            // Element age = head.addElement("age");
            //age.addText("18");

            // 4、格式化模板
            //OutputFormat format = OutputFormat.createCompactFormat();
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("GBK");

            // 5、生成xml文件
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            try {
                XMLWriter writer = new XMLWriter(out, format);
                writer.write(document);
                writer.close();
            } catch (IOException e) {
                System.out.println("生成xml文件失败。文件名【" + fileName + "】");
            }

            // 6、生成的XML文件
            // 7、利用文件输出流输出到文件， 文件输出到了您的项目根目录下了
            try (FileOutputStream fos = new FileOutputStream(fileName + ".xml")) {
                fos.write(out.toByteArray());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
            beginDate = dateAddOne(beginDate,1);

        }

       // System.out.println("生成采购xml文件成功");
    }



    @Override
    @Async
    public void createExcelPurReturnYJ(Date beginDate, Date endDate) throws ParseException {
        purReturn(beginDate,endDate,"50301");
        purReturn(beginDate,endDate,"0");
    }


    private void purReturn(Date beginDate, Date endDate,String stockNo) throws ParseException {

        while(beginDate.compareTo(endDate)<0) {
        List<Map<String, Object>> saleYJHList = null;


            saleYJHList = gdjfMapper.selectExcelReturnPurYJH(beginDate, dateAddOne(beginDate,1));


        if(saleYJHList.size()>0) {

            //List<Map<String,Object>> saleYJDList = jmgdMapper.selectExcelSaleYJD(beginDate,endDate);
            SimpleDateFormat format3 = new SimpleDateFormat("yyyyMMdd");

            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");

            // 1.声明文件名称
            String fileName = "tuihuochuku_jiangmen";
            //String fileName1 = "销售出库";

            String stockName = "";
            //String fileName1 = "销售出库";

            if (stockNo.equals("50301")) {
                stockName = "零售仓";
                fileName = "零售" + fileName;
            } else {
                stockName = "批发仓";
                fileName = "批发" + fileName;
            }


            fileName = fileName + format3.format(beginDate);
            fileName = "D:\\GDFiles\\Yj\\JF\\" + stockName + "\\" + fileName;


            // 2.创建dom对象
            Document document = DocumentHelper.createDocument();

            // 3.添加节点，根据需求添加，这里我只是设置了一个head节点，下面有name和age两个子节点
            Element head = document.addElement("jh", "http://cmsland.com/yjj_qyjk_thck.xsd");
            //head.addAttribute("xmlns", "http://cmsland.com/yjj_qyjk_pfck.xsd");
            for (Map<String, Object> map : saleYJHList) {
                System.out.println(map.get("SALENO") == null ? "" : map.get("SALENO").toString());
                String[] arr1 = new String[10];//有几列就写几
                //如果数据不为空则赋值(这里就是循环每行的数据)
                Element dj = head.addElement("dj");
                dj.addAttribute("djbh", map.get("SALENO").toString());
                dj.addAttribute("ckrq", format1.format(format2.parse(map.get("SALE_DATE").toString())));
                dj.addAttribute("ghqy", map.get("VENCUS_NAME").toString());


                List<Map<String, Object>> saleYJDList = gdjfMapper.selectExcelReturnPurYJD(map.get("SALENO").toString(), format2.parse(map.get("SALE_DATE").toString()));
                for (Map<String, Object> map1 : saleYJDList) {
                    Element tm = dj.addElement("tm");
                    tm.addAttribute("bwm", map1.get("BWM") == null ? "" : map1.get("BWM").toString());
                    tm.addAttribute("bzgg", map1.get("WARE_SPEC") == null ? "" : map1.get("WARE_SPEC").toString());
                    tm.addAttribute("scph", map1.get("MAKENO") == null ? "" : map1.get("MAKENO").toString());
                    tm.addAttribute("scrq", map1.get("PROD_DATE") == null ? "" : format1.format(format2.parse(map1.get("PROD_DATE").toString())));
                    tm.addAttribute("yxqz", map1.get("VALID_DATE") == null ? "" : format1.format(format2.parse(map1.get("VALID_DATE").toString())));
                    tm.addAttribute("sl", map1.get("WARE_QTY") == null ? "" : map1.get("WARE_QTY").toString());
                    tm.addAttribute("xsdh", map1.get("SALENO") == null ? "" : map1.get("SALENO").toString());
                }
/*

            arr1[0] = map.get("SALE_DATE") == null ? "" : format1.format(format2.parse(map.get("SALE_DATE").toString()));
            arr1[1] = map.get("SALENO") == null ? "" : map.get("SALENO").toString();
            arr1[2] = map.get("VENCUS_NAME") == null ? "" : map.get("VENCUS_NAME").toString();
            arr1[3] = map.get("SALER") == null ? "" : map.get("SALER").toString();
            arr1[4] = map.get("TRANS_TYPE") == null ? "" : map.get("TRANS_TYPE").toString();
            arr1[5] = map.get("CHENG_YUN_PERSON") == null ? "" : map.get("CHENG_YUN_PERSON").toString();
            arr1[6] = map.get("BEGIN_DATE") == null ? "" : format1.format(format2.parse(map.get("BEGIN_DATE").toString()));
            arr1[7] = map.get("END_DATE") == null ? "" : format1.format(format2.parse(map.get("END_DATE").toString()));
            arr1[8] = map.get("STARTUP_TEMP") == null ? "" : map.get("STARTUP_TEMP").toString();
            arr1[9] = map.get("ARRI_TEMP") == null ? "" : map.get("ARRI_TEMP").toString();*/
            }


            // Element age = head.addElement("age");
            //age.addText("18");

            // 4、格式化模板
            //OutputFormat format = OutputFormat.createCompactFormat();
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("GBK");

            // 5、生成xml文件
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            try {
                XMLWriter writer = new XMLWriter(out, format);
                writer.write(document);
                writer.close();
            } catch (IOException e) {
                System.out.println("生成xml文件失败。文件名【" + fileName + "】");
            }

            // 6、生成的XML文件
            // 7、利用文件输出流输出到文件， 文件输出到了您的项目根目录下了
            try (FileOutputStream fos = new FileOutputStream(fileName + ".xml")) {
                fos.write(out.toByteArray());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
            beginDate = dateAddOne(beginDate,1);

        }

       // System.out.println("生成采购退货xml文件成功");
    }


    @Override
    @Async
    public void createExcelStockYJ(Date beginDate) throws ParseException {
        stock(beginDate,"0");
        stock(beginDate,"50301");

    }


    private void stock(Date beginDate,String stockNo) throws ParseException {
        List<Map<String, Object>> saleYJHList = null;
        //List<Map<String,Object>> saleYJDList = jmgdMapper.selectExcelSaleYJD(beginDate,endDate);


            saleYJHList = gdjfMapper.selectStock(beginDate);


        if(saleYJHList.size()>0) {
            SimpleDateFormat format3 = new SimpleDateFormat("yyyyMMdd");

            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");

            // 1.声明文件名称
            String fileName = "kucunjilu_jiangmen";
            //String fileName1 = "销售出库";

            String stockName = "";
            //String fileName1 = "销售出库";
            if (stockNo.equals("50301")) {
                stockName = "零售仓";
            } else {
                stockName = "批发仓";
            }


            fileName = fileName + format3.format(new Date());
            fileName = "D:\\GDFiles\\Yj\\JF\\" + stockName + "\\" + fileName;


            // 2.创建dom对象
            Document document = DocumentHelper.createDocument();

            // 3.添加节点，根据需求添加，这里我只是设置了一个head节点，下面有name和age两个子节点
            Element head = document.addElement("dj", "http://cmsland.com/yjj_qyjk_kc.xsd");
            //head.addAttribute("xmlns", "http://cmsland.com/yjj_qyjk_pfck.xsd");
            head.addAttribute("pdsj", saleYJHList.get(0).get("SALE_DATE") == null ? "" : format1.format(format2.parse(saleYJHList.get(0).get("SALE_DATE").toString())));

            if (saleYJHList.size() > 0) {
                String[] arr1 = new String[10];//有几列就写几
                //如果数据不为空则赋值(这里就是循环每行的数据)
                //Element dj = head.addElement("dj");
                //dj.addAttribute("pdsj",saleYJHList.get(0).get("SALE_DATE") == null ? "" : format1.format(format2.parse(saleYJHList.get(0).get("SALE_DATE").toString())));

                //List<Map<String,Object>> saleYJDList = jmgdMapper.selectExcelReturnSaleYJD(map.get("SALENO") == null ? "" : map.get("SALENO").toString());
                for (Map<String, Object> map1 : saleYJHList) {
                    Element tm = head.addElement("tm");
                    tm.addAttribute("bwm", map1.get("BWM") == null ? "" : map1.get("BWM").toString());
                    tm.addAttribute("bzgg", map1.get("WARE_SPEC") == null ? "" : map1.get("WARE_SPEC").toString());
                    tm.addAttribute("kcsl", map1.get("WARE_QTY") == null ? "" : map1.get("WARE_QTY").toString());
                    tm.addAttribute("scph", map1.get("MAKENO") == null ? "" : map1.get("MAKENO").toString());
                    tm.addAttribute("scrq", map1.get("PROD_DATE") == null ? "" : format1.format(format2.parse(map1.get("PROD_DATE").toString())));
                    tm.addAttribute("yxqz", map1.get("VALID_DATE") == null ? "" : format1.format(format2.parse(map1.get("VALID_DATE").toString())));
                }
/*

            arr1[0] = map.get("SALE_DATE") == null ? "" : format1.format(format2.parse(map.get("SALE_DATE").toString()));
            arr1[1] = map.get("SALENO") == null ? "" : map.get("SALENO").toString();
            arr1[2] = map.get("VENCUS_NAME") == null ? "" : map.get("VENCUS_NAME").toString();
            arr1[3] = map.get("SALER") == null ? "" : map.get("SALER").toString();
            arr1[4] = map.get("TRANS_TYPE") == null ? "" : map.get("TRANS_TYPE").toString();
            arr1[5] = map.get("CHENG_YUN_PERSON") == null ? "" : map.get("CHENG_YUN_PERSON").toString();
            arr1[6] = map.get("BEGIN_DATE") == null ? "" : format1.format(format2.parse(map.get("BEGIN_DATE").toString()));
            arr1[7] = map.get("END_DATE") == null ? "" : format1.format(format2.parse(map.get("END_DATE").toString()));
            arr1[8] = map.get("STARTUP_TEMP") == null ? "" : map.get("STARTUP_TEMP").toString();
            arr1[9] = map.get("ARRI_TEMP") == null ? "" : map.get("ARRI_TEMP").toString();*/
            }


            // Element age = head.addElement("age");
            //age.addText("18");

            // 4、格式化模板
            //OutputFormat format = OutputFormat.createCompactFormat();
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("GBK");

            // 5、生成xml文件
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            try {
                XMLWriter writer = new XMLWriter(out, format);
                writer.write(document);
                writer.close();
            } catch (IOException e) {
                System.out.println("生成xml文件失败。文件名【" + fileName + "】");
            }

            // 6、生成的XML文件
            // 7、利用文件输出流输出到文件， 文件输出到了您的项目根目录下了
            try (FileOutputStream fos = new FileOutputStream(fileName + ".xml")) {
                fos.write(out.toByteArray());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

      //  System.out.println("生成仓库xml文件成功");
    }


    public static void main(String[] args){
        //System.out.println(dateAddOne(new Date()));


        Date begin = new Date();
        begin = dateAddOne(begin,-10);
        Date end = new Date();
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
       /* System.out.println(format1.format(begin));
        begin = dateAddOne(begin);

        System.out.println(format1.format(begin));*/


        while(begin.compareTo(end)<=0){
            System.out.println(format1.format(begin));
            begin = dateAddOne(begin,1);
        }


    }

    /*日期加+1天*/
    public static Date dateAddOne(Date date,int day) {
        Calendar   calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE,day); //把日期往后增加一天,整数  往后推,负数往前移动
        date=calendar.getTime(); //这个时间就是日期往后推一天的结果
        return date;

    }


}
